﻿using EquipmentManager.Entities;
using EquipmentManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EquipmentManager.BOLogic
{
    public class PostOfficeBO : BOBase
    {
        public List<PostOffice> GetPostOffices(long RegionId)
        {
            try
            {
                var postOffices = db.Set<PostOffice>().Where(p => p.Region.Id == RegionId && 
                    p.Status == EntityStatus.Active).OrderBy(p => p.Name).ToList();
                return postOffices;
            }
            catch (Exception ex)
            {
                return new List<PostOffice>();
            }
        }

        public List<PostOffice> GetOperPostOffices()
        {
            try
            {
                var postOffices = db.Set<PostOffice>().Where(p => p.Region.Id == User.Region.Id && 
                    p.Status == EntityStatus.Active).OrderBy(p => p.Name).ToList();
                return postOffices;
            }
            catch (Exception ex)
            {
                return new List<PostOffice>();
            }
        }

        public PostOffice GetPostOffice(long PostOfficeId)
        {
            try
            {
                var postOffice = db.Set<PostOffice>().Single(p => p.Id == PostOfficeId);

                return postOffice;
            }
            catch (Exception ex)
            {
                return new PostOffice();
            }
        }

        public bool CreateNewPostOffice(CreateModel<PostOffice> model)
        {
            try
            {
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    model.Instance.Status = EntityStatus.Active;
                    model.Instance.Region = db.Set<Region>().Single(r => r.Id == model.ParentId);

                    db.Set<PostOffice>().Add(model.Instance);
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool EditPostOffice(CreateModel<PostOffice> model)
        {
            try
            {
                var postOffice = db.Set<PostOffice>().Single(c => c.Id == model.Instance.Id);
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    // можно создавать новую измененную, а эту делать неактивной если что;)
                    postOffice.Name = model.Instance.Name;
                    postOffice.Description = model.Instance.Description;

                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeletePostOffice(long PostOfficeId)
        {
            try
            {
                var postOffice = db.Set<PostOffice>().Single(c => c.Id == PostOfficeId);
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    postOffice.Status = EntityStatus.Deleted;
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}